package archer.framework.common.repository.condition;

import archer.framework.utils.ValidateUtils;
import jodd.json.meta.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询条件
 *
 * @author christ
 * @date 2016/4/20
 */
public class QueryCondition {

    private List<QueryItem> queryItems;

    private List<OrderByItem> orderByItems;

    private Integer pageIndex;

    private Integer pageSize;

    public List<QueryItem> getQueryItems() {
        return queryItems;
    }

    /**
     * 获得某个queryItem
     *
     * @param field
     * @return
     */
    @JSON(include = false)
    public QueryItem getQueryItem(String field) {
        if (ValidateUtils.isNotEmpty(queryItems)) {
            for (QueryItem item : queryItems) {
                if (field.equals(item.getField())) {
                    return item;
                }
            }
        }
        return null;
    }

    @JSON(include = false)
    public OrderByItem getOrderByItem(String field){
        if (ValidateUtils.isNotEmpty(orderByItems)) {
            for (OrderByItem item : orderByItems) {
                if (field.equals(item.getField())) {
                    return item;
                }
            }
        }
        return null;
    }

    /**
     * 获得某个queryItem对应的value
     *
     * @param field
     * @return
     */
    @JSON(include = false)
    public Object getQueryItemValue(String field) {
        QueryItem item = getQueryItem(field);
        return item == null ? item : item.getValue();
    }

    /**
     * 获得某个queryItem对应的op
     *
     * @param field
     * @return
     */
    @JSON(include = false)
    public String getQueryItemOp(String field) {
        QueryItem item = getQueryItem(field);
        return item == null ? null : item.getOp();
    }

    @JSON(include = false)
    public QueryCondition addQueryItem(QueryItem queryItem) {
        if (queryItems == null) {
            queryItems = new ArrayList<>();
        }
        queryItems.add(queryItem);
        return this;
    }

    @JSON(include = false)
    public QueryCondition addOrderByItem(OrderByItem orderByItem) {
        if (orderByItems == null) {
            orderByItems = new ArrayList<>();
        }
        orderByItems.add(orderByItem);
        return this;
    }

    public QueryCondition setQueryItems(List<QueryItem> queryItems) {
        this.queryItems = queryItems;
        return this;
    }

    public List<OrderByItem> getOrderByItems() {
        return orderByItems;
    }

    public QueryCondition setOrderByItems(List<OrderByItem> orderByItems) {
        this.orderByItems = orderByItems;
        return this;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public QueryCondition setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
        return this;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public QueryCondition setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        return this;
    }
}
